package com.t.bricks.bussiness.db.dao.customeraccount.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.customeraccount.CustomerAccountDao;
import com.t.bricks.bussiness.db.entity.customeraccount.CustomerAccount;
import com.t.bricks.bussiness.db.mapper.customeraccount.CustomerAccountMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class CustomerAccountDaoImpl implements CustomerAccountDao<CustomerAccount> {

	@Autowired
	private CustomerAccountMapper customerAccountMapper;

	/**
	 * 修改密码
	 * @param sId
	 * @param sOldPass
	 * @param sNewPass
	 * @param iVersion
	 * @param dModifiedDate
	 * @param sModifieder
	 * @param sRecordKey
	 * @return
	 */
	@Override
	@Transactional
	public MsgEmity editPass(String sId, String sOldPass, String sNewPass,
			int iVersion, Date dModifiedDate, String sModifieder, String sRecordKey) {
		try {
			int i = customerAccountMapper.editPass(sId, sOldPass, sNewPass, iVersion, dModifiedDate, sModifieder, sRecordKey);
			if (i < 1) {
				Log.error("修改未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "修改失败!", 7001);
			}
			
			i = customerAccountMapper.addLog(sId, sOldPass, sNewPass, "支付密码", sModifieder);
			if (i < 1) {
				Log.error("添加日志未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "修改失败!", 7002);
			}
		} catch (Exception e) {
			Log.error("修改时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "修改时发生异常!", 7003);
		}
		
		return new MsgEmity(true, "修改成功!", 7999);
	}

	/**
	 * 修改手势密码
	 * @param sId
	 * @param sOldPass
	 * @param sNewPass
	 * @param iVersion
	 * @param dModifiedDate
	 * @param sModifieder
	 * @param sRecordKey
	 * @return
	 */
	@Override
	@Transactional
	public MsgEmity editSignPass(String sId, String sOldPass, String sNewPass,
			int iVersion, Date dModifiedDate, String sModifieder, String sRecordKey) {
		try {
			int i = customerAccountMapper.editSignPass(sId, sOldPass, sNewPass, iVersion, dModifiedDate, sModifieder, sRecordKey);
			if (i < 1) {
				Log.error("修改未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "修改失败!", 7001);
			}
			
			i = customerAccountMapper.addLog(sId, sOldPass, sNewPass, "支付手势密码", sModifieder);
			if (i < 1) {
				Log.error("添加日志未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "修改失败!", 7002);
			}
		} catch (Exception e) {
			Log.error("修改时发生异常:", e.getMessage());
			return new MsgEmity(false, "修改时发生异常!", 7003);
		}
		
		return new MsgEmity(true, "修改成功!", 7999);
	}
	
	/**
	 * 更新顾客账户中的余额
	 * @param customerAccount
	 * @return
	 */
	@Override
	public MsgEmity changeFund(CustomerAccount customerAccount) {
		try {
			int i = customerAccountMapper.changeFund(customerAccount);
			if (i < 1) {
				Log.error("更新顾客余额未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "更新顾客余额失败!", 7001);
			}
		} catch (Exception e) {
			Log.error("更新顾客余额时发生异常:", e.getMessage());
			return new MsgEmity(false, "更新顾客余额时发生异常!", 7002);
		}
		
		return new MsgEmity(true, "更新顾客余额成功!", 7999);
	}
	
	/**
	 * 更新顾客账户中的积分
	 * @param customerAccount
	 * @return
	 */
	@Override
	public MsgEmity changeTally(CustomerAccount customerAccount) {
		try {
			int i = customerAccountMapper.changeTally(customerAccount);
			if (i < 1) {
				Log.error("更新顾客积分未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "更新顾客积分失败!", 7001);
			}
		} catch (Exception e) {
			Log.error("更新顾客积分时发生异常:", e.getMessage());
			return new MsgEmity(false, "更新顾客积分时发生异常!", 7002);
		}
		
		return new MsgEmity(true, "更新顾客积分成功!", 7999);
	}
	
	/**
	 * 取用户的登录密码
	 * @param uId
	 * @return
	 */
	@Override
	public MsgEmity findLoginPass(String uId) {
		try {
			String sPass = customerAccountMapper.findLoginPass(uId);
			if (null == sPass) {
				return new MsgEmity(false, "获取操作用户密码失败", 7001);
			}
			
			return new MsgEmity(true, "获取操作用户密码成功!", sPass);
		} catch (Exception e) {
			Log.error("获取操作用户密码发生异常:", e.getMessage());
			return new MsgEmity(false, "获取操作用户密码发生异常!", 7002);
		}
	}

}
